package com.leetcode.math;

public class MyPow {

    public static void main(String[] args) {
        MyPow myPow = new MyPow();
        double x = 2.00000;
        int n = -2147483648;
        System.out.println(myPow.myPow(x, n));
    }

    public double myPow(double x, int n) {
        long pw = n;
        if (pw == 0) {
            return 1.0;
        }
        boolean isNegative = false;
        if (pw < 0) {
            isNegative = true;
            pw = pw * -1;
        }
        double tmp = x;
        double res = 1;
        while(pw > 1) {
            if (pw % 2 == 1) {
                res *= tmp;
                pw--;
            }
            tmp *= tmp;
            pw /= 2;
        }
        res *= tmp;
        if (isNegative) {
            res = 1.0 /res;
        }
        return res;
    }


}
